<?php
namespace Index\Controller;

class IndexController extends EmptyController {

	 public function index(){
	 	echo 44;
	 	ini_set('session.gc_maxlifetime', "time() + 3600");
	 	if (isset($_GET['code'])) 
	 	{
	 			$dir = dirname(__FILE__);
	 			$duzhi = $dir.'\refresh_token.txt';
	 			$open = fopen($duzhi, "r+");
	 			$read = fread($open,filesize($duzhi));
	 			preg_match_all("/refresh_token=>(.+)/i", $read, $zread);
	 			preg_match_all("/create_time=>(.+)/i",$read,$zcrea);
	 			$refresh_token = $zread[1][0];
	 			$create_time = $zcrea[1][0];
	 			fclose($open);
	 			if (empty($read) || empty($refresh_token) || time()-$create_time > '7000') 
	 			{
	 				$code = $_GET['code'];//获取code 
	 				$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx836fc5841f715e8c&secret=df31249b48bc87fc2d7d98e0cb2da9c4&code=".$code."&grant_type=authorization_code";
	 			/*$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx703acc3701dcc383&secret=8d2116f8ceb66da483f9bee5da33b5ed&code=".$code."&grant_type=authorization_code";*/
	 				$weixin = $this -> http_request($url);
					$jsondecode = json_decode($weixin);  
					$array = get_object_vars($jsondecode);
					$openid = $array['openid'];
					$refresh_token = $array['refresh_token'];
					$data = 'refresh_token=>'.$refresh_token."\n".'create_time=>'.time();
						
					$open = fopen($duzhi, "w");
					fwrite($open, $data);
					fclose($open);
	 			}
	 			
				

			$url2 = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx836fc5841f715e8c&grant_type=refresh_token&refresh_token=".$refresh_token;

			$weixin2 = $this -> http_request($url2);
			$weixin = file_get_contents($url);
			$jsondecode2 = json_decode($weixin2);
			$array2 = get_object_vars($jsondecode2);
			$access_token = $array2['access_token'];

			//$sinfo = "https://api.weixin.qq.com/sns/auth?access_token=".$access_token."&openid=".$openid;
			$sinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."lang=zh_CN";
			$si = $this -> http_request($sinfo);
			//$weixin = file_get_contents($url);//通过code换取网页授权access_token 
			$sjson = json_decode($si); //对JSON格式的字符串进行编码  
			$sarray = get_object_vars($sjson);
			$sopenid = $sarray['openid'];
			$u_arr=M('user')->where(array('openid'=>$sopenid))->find();
			if(!empty($u_arr)){
	             	$_SESSION['user_info']=$u_arr;
	             
	             	$this->redirect('User/index');
	     	}
	     	else
	     	{
	     		$this -> lock($sopenid);
	     		die;
	     	}
		//print_r($sarray);
	 	}
	 	if (empty($_SESSION['user_info'])) 
	 	{
	 		$this->backURL=I('backURL');
	 	
	    	$this->display();
	 	}
	 	else
	 	{
	 		$this->redirect('User/index');
	 	}
	    
	
	 }
	 
	 public function handle_login(){
	 
	     $act=I('post.act');
	     
	     if($act=='add'){
	         
	         $mobile=I('post.mobile');
	         $upwd=I('post.upwd');
	         $backURL=I('post.backURL');
	         !is_empty($mobile,$upwd)?message(2,L('_IS_EMPTY_')):'';
	         
	         $u_arr=M('user')->where(array('mobile'=>$mobile,'upwd'=>md5($upwd.C('SALT'))))->find();
	         if(!empty($u_arr)){
	             
	             $_SESSION['user_info']=$u_arr;
	             
	             message(1,L('_DO_SUCCEED_'),!empty($backURL)?U(''.str_replace('-','/',$backURL).''):U('User/index'));
	             
	         }
	         else{
	             
	             message(2,L('_FALSE_USR_PWD_'));
	             
	         }
	         
	         
	     }
	     
	     message(2,L('_DO_FAILED_'));
	 }

	 
	  protected function http_request($url, $data = null)
 		{
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_URL, $url);
    		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    		if (!empty($data)) {
        			curl_setopt($curl, CURLOPT_POST, 1);
        			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    		}
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    		$info = curl_exec($curl);
    		curl_close($curl);
    		return $info;
 		}

 		public function lock($openid)
 		{
 			$this -> assign('openid',$openid);
 			$this -> display('Index_lock');
 		}

 		public function lock_info()
 		{
 			ini_set('session.gc_maxlifetime', "time() + 3600");
 			$openid = trim(I('post.openid'));
 			$mobile = trim(I('post.mobile'));
 			$upwd = trim(I('post.upwd'));
 			if (empty($mobile)&&empty($openid)) 
 			{
 				$this -> index();
 			}
 			else if (empty($openid)) 
 			{
 				$this -> index();
 			}
 			else if (empty($mobile)) 
 			{
 				$this -> lock($openid);
 			}
 			else
 			{
 				$num = M('user')->where(['mobile' => $mobile , 'upwd' => md5($upwd.C('SALT'))])->find();
 				if (empty($num)) 
 				{
 					$this -> lock($openid);
 				}
 				else
 				{
 					$m = new \Think\Model;
 					$add = $m -> execute("update y_user set openid=\"$openid\" where mobile = \"$mobile\"");
 					$_SESSION['user_info']=$num;
	            	$this->redirect('User/index');

 				}
 			}
 		}

 		public function forget()
 		{
 			$mobile = trim(I('mobile'));
 			if (!empty($mobile)) 
 			{
 				$m = new \Think\Model;
 				$num = $m -> query("select uid from y_user where mobile = \"$mobile\"");
 				$id = $num[0]['uid'];
 				if (empty($id)) 
 				{
 					$this -> ajaxReturn('电话号码错误');
 				}
 				$create_time = time();
 				$add = $m -> execute("insert into y_forget(user_id,create_time) values(\"$id\",\"$create_time\")");
 				$this -> ajaxReturn('已传入信息');
 			}
 			$this -> display();
 		}

}